// This file contains the main logic for the flying bird game, including bird flight, collision detection, and scoring system.

const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');

let bird = {
    x: 50,
    y: canvas.height / 2,
    width: 30,
    height: 30,
    gravity: 0.6,
    lift: -15,
    velocity: 0
};

let isGameOver = false;
let score = 0;

function setup() {
    canvas.width = 400;
    canvas.height = 600;
    document.addEventListener('keydown', fly);
    requestAnimationFrame(gameLoop);
}

function fly(event) {
    if (event.code === 'Space' && !isGameOver) {
        bird.velocity += bird.lift;
    }
}

function update() {
    bird.velocity += bird.gravity;
    bird.y += bird.velocity;

    if (bird.y + bird.height >= canvas.height) {
        isGameOver = true;
    }

    if (bird.y < 0) {
        bird.y = 0;
        bird.velocity = 0;
    }
}

function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.fillStyle = 'skyblue';
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    ctx.fillStyle = 'yellow';
    ctx.fillRect(bird.x, bird.y, bird.width, bird.height);

    ctx.fillStyle = 'black';
    ctx.font = '20px Arial';
    ctx.fillText(`Score: ${score}`, 10, 20);
}

function gameLoop() {
    if (!isGameOver) {
        update();
        draw();
        score++;
        requestAnimationFrame(gameLoop);
    } else {
        ctx.fillStyle = 'red';
        ctx.fillText('Game Over', canvas.width / 2 - 50, canvas.height / 2);
    }
}

setup();